package com.situ.meetingRoom.controller;

import com.situ.meetingRoom.util.FileUploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(name = "UploadServlet", value = "/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {

    private static final String PATH = "D:/upload";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取图片
        String head = req.getParameter("head");
        FileUploadUtil.download(head, PATH, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //上传图片
        //从request中获取，文件
        Part file = req.getPart("file");
        String fileName = FileUploadUtil.upload(file, PATH);

        //返回json格式的数据
        resp.setContentType("application/json; charset=UTF-8");
        resp.getWriter().append("{\"code\": 0, \"msg\": \"" + fileName + "\"}");
    }
}
